<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>增删课程</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/static/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/layuiadmin/style/admin.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/layuiadmin/style/template.css}" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                                <button class="layui-btn layuiadmin-btn-list" data-type="importData" id="importData"
                                        onclick="javascript:return false;">通过pdf导入课程
                                </button>

            </div>
            <!--表单主体部分-->
            <table id="studentInfoList" lay-filter="studentInfo">

            </table>

            <div id="TeacherTableBar" style="display: none;">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </div>

            <script type="text/html" id="table-content-list">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script th:src="@{/static/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table', 'laypage', 'upload', 'layer', 'laydate', 'jquery', 'form'], function () {

        var $ = layui.$
            , table = layui.table
            , form = layui.form
            , upload = layui.upload
            , layer = layui.layer;

        var laypage = layui.laypage;

        //方法级渲染
        table.render({
            elem: '#studentInfoList'
            , id: 'studentInfoList'
            , url: '/back/book/getAllBooks'
            , title: '课程列表'
            // ,toolbar: '#bwgzfkToolBar'
            , cellMinWidth: 200
            , page: true
            , limit: 20
            , height: 'full-221'
            , cols: [
                [
                    {checkbox: true, fixed: true}
                    , {type: 'id', field: 'book_name', title: '课程名称', width: 400, fixed: 'left', align: "center"}
                    , {title: '操作', toolbar: '#table-content-list', width: 400, align: 'center'}
                ]
            ]
            , parseData: function (res) {
                return {
                    "code": 0,
                    "msg": res.msg,
                    "data": res.payload.content, //返回值 json 的key
                    "count": res.payload.total
                }
            }
        });

        //excel批量导入
        const uploadListIns = upload.render({
            elem: '#importData'
            , url: '/JsMind/AddBook' //改成您自己的上传接口
            , accept: 'file'
            , exts: 'pdf'
            , multiple: false
            , auto: true
            , done: function (Msg) {
                if (Msg.code === 100) {
                    // window.parent.location.reload();  //刷新父页面
                    layer.msg("导入成功", {icon: 6})
                    setTimeout(()=>{
                        window.location.reload();   //刷新子页面
                    },1000)

                } else {
                    layer.msg("导入失败", {icon: 2})
                }
            }
            , error: function (index, upload) {

            }
        });

        // 监听搜索
        // form.on('submit(student-search)', function (data) {
        //     table.reload('studentInfoList', {
        //         url: '/back/conditionSearch',
        //         method: 'post',
        //         page: {
        //             curr: 1 //从第一页开始
        //         },
        //         where: { //where 是指你要往后台发送的参数的键值对，可以不加key
        //             studentNumber: data.field.studentNumber,
        //             name: data.field.name,
        //             classes: data.field.classes
        //         }
        //     });
        // });

        // 修改和删除等操作
        //监听行工具事件
        table.on('tool(studentInfo)', function (obj) {
            const data = obj.data;
            const book_name = data.book_name;

            if (obj.event === 'del') {
                // 删除信息
                layer.confirm('真的要删除这一门课程吗', function (index) {
                    $.ajax({
                        type: 'GET',
                        async: true,
                        url: "/back/book/deleteBookAbout",
                        dataType: 'JSON',
                        data: {
                            book_name: book_name
                        },
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg('删除成功', {
                                    time: 1000, icon: 6
                                })
                                setTimeout(()=>{
                                    window.location.reload();   //刷新子页面
                                },1000)
                            } else {
                                layer.msg(res.msg, {time: 1000, icon: 5})
                                setTimeout(()=>{
                                    window.location.reload();   //刷新子页面
                                },1000)
                            }
                        },
                        error: function (data) {
                            layer.msg("删除时发生错误，请重试...", {time: 1000, icon: 7})
                            return false;
                        }
                    })
                    obj.del();
                    layer.close(index);
                });

            }

            //判断浏览器大小方法
            function screen() {
                //获取当前窗口的宽度
                var width = $(window).width();
                if (width > 768) {
                    return 1;   //小屏幕
                } else {
                    return 0;   //超小屏幕
                }
            }

            $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

        });
    });
</script>
</body>
</html>
